package com.godme.concurrent.pattern;

import java.util.function.Supplier;

public class FuturePattern {

    static class Future<T> extends Thread {
        private Supplier<T> supplier;
        private T result;

        public Future(Supplier<T> supplier){
            this.supplier = supplier;
            start();
        }

        @Override
        public synchronized void run() {
            result = supplier.get();
            notify();
        }

        public synchronized T get() throws InterruptedException {
            if(result == null){
                wait();
            }
            return result;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Future<String> stringFuture = new Future<>(() -> {
            try{
                Thread.sleep(3000);
            }catch (Exception e){
                e.printStackTrace();
            }
            return "godme";
        });
        System.err.println(stringFuture.get());
    }
}
